//
//  oldMessageListViewController.m
//  laonianbang
//
//  Created by 勇拓 李 on 18/4/3.
//  Copyright © 2018年 zaiyou. All rights reserved.
//

#import "oldMessageListViewController.h"
#import "oldMessageListTableViewCell.h"
#import "oldMomentDetalViewController.h"

@interface oldMessageListViewController ()
@property (nonatomic, copy)NSArray *dataSource;
@end

static NSString *const identifier = @"oldMessageListTableViewCell";

@implementation oldMessageListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initView];
    [self initData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - initView
- (void)initView
{
    self.navigationItem.title = @"消息中心";
    
    [self rigisterCell:identifier];
}

#pragma mark - initData
- (void)initData
{
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(refresh)];
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(loadMore)];
    [self.tableView.mj_header beginRefreshing];
}

- (void)refresh
{
    NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"1", @"pageNum", @"10", @"pageSize", nil];
    
    [self loadData:params ifResetPageNo:YES];
}

- (void)loadMore
{
    NSString *pageNum = [NSString stringWithFormat:@"%ld", (long)pageNo + 1];
    
    NSMutableDictionary *params = [NSMutableDictionary dictionaryWithObjectsAndKeys:pageNum, @"pageNum", @"10", @"pageSize", nil];
    
    [self loadData:params ifResetPageNo:NO];
}

- (void)loadData:(NSDictionary *)params ifResetPageNo:(BOOL)reset
{
    WeakSelf
    
    [OLDNetWork httpRequestWithPostPut:URL_GetFeedMessageList params:params success:^(id data) {
        if (reset) {
            pageNo = 1;
            weakSelf.dataSource = [NSArray array];
            [weakSelf.tableView.mj_header endRefreshing];
        }
        else
        {
            pageNo ++;
            [weakSelf.tableView.mj_footer endRefreshing];
        }
        
        [weakSelf initDataSource:data];
        [weakSelf.tableView reloadData];
        
    } error:^(NSString *error) {
        if (reset) {
            [weakSelf.tableView.mj_header endRefreshing];
        }
        else
        {
            [weakSelf.tableView.mj_footer endRefreshing];
        }
        [oldHub warningUser:error];
    }];
}

- (void)initDataSource:(id)data
{
    NSMutableArray *array = [_dataSource mutableCopy];
    
    for (int i = 0; i < [data count]; i++) {
        oldMomentMessage *message = [[oldMomentMessage alloc] initWithDictionary:data[i]];
        [array addObject:message];
    }
    
    _dataSource = array;
}


#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return [_dataSource count];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    oldMessageListTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier forIndexPath:indexPath];
    
    if (cell == nil) {
        cell = [[oldMessageListTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    
    cell.message = _dataSource[indexPath.row];
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    oldMomentDetalViewController *vc = [oldMomentDetalViewController new];
    oldMomentMessage *message = _dataSource[indexPath.row];
    vc.fid = message.fid;
    vc.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:vc animated:YES];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 60;
}

@end
